package com.example.administrator.dagger2demo.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.example.administrator.dagger2demo.R;
import com.example.administrator.dagger2demo.adapter.drag.OnStartDragListener;
import com.example.administrator.dagger2demo.adapter.drag.SimpleItemTouchHelperCallback;
import com.example.administrator.dagger2demo.adapter.inter.Indo;
import com.example.administrator.dagger2demo.adapter.inter.WorkAdapter;
import com.example.administrator.dagger2demo.adapter.listener.OnItemClickListener;
import com.example.administrator.dagger2demo.adapter.listener.OnItemLongClickListener;
import com.example.administrator.dagger2demo.ui.index.IndexContract;
import com.example.administrator.dagger2demo.view.NavigationTabBar;
import com.example.administrator.dagger2demo.widget.HidingScrollListener;

import java.util.ArrayList;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import rx.Observer;

/**
 * Created by Administrator on 2015/11/18.
 */
public class FragmentI extends BaseLazyLoadFragment implements OnStartDragListener {
    RecyclerView workRecyclerView;
    WorkAdapter workAdapter;
    PtrClassicFrameLayout mPtrFrame;
    ArrayList<Indo> indos = new ArrayList<>();
    public static IndexContract.IndexActionsListener mIndexActionsListener;

    public static FragmentI newInstance(Bundle args, IndexContract.IndexActionsListener indexActionsListener) {
        FragmentI pageFragment = new FragmentI();
        pageFragment.setArguments(args);
        mIndexActionsListener = indexActionsListener;
        return pageFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //1.
        super.onCreate(savedInstanceState);
        Log.e("TAG", "onCreate=I");
    }

    FrameLayout frameLayout;
    View bg_ntb_horizontal;
    NavigationTabBar navigationTabBar;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //2.
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_i, container, false);
        frameLayout = (FrameLayout) getActivity().findViewById(R.id.wrapper_ntb_horizontal);
        bg_ntb_horizontal = (View) getActivity().findViewById(R.id.bg_ntb_horizontal);
        navigationTabBar = (NavigationTabBar) getActivity().findViewById(R.id.ntb_horizontal);
//        final NavigationTabBar ntbSample5 = (NavigationTabBar)view. findViewById(R.id.ntb_sample_5);
//        final ArrayList<NavigationTabBar.Model> models5 = new ArrayList<>();
//        models5.add(new NavigationTabBar.Model(
//                getResources().getDrawable(R.mipmap.icon_1460189176668), Color.WHITE));
//        models5.add(new NavigationTabBar.Model(
//                getResources().getDrawable(R.mipmap.icon_1460187505919), Color.WHITE));
//        ntbSample5.setModels(models5);
//        ntbSample5.setModelIndex(2, true);
//
//        ntbSample5.setOnTabBarSelectedIndexListener(new NavigationTabBar.OnTabBarSelectedIndexListener() {
//            @Override
//            public void onStartTabSelected(final NavigationTabBar.Model model, final int index) {
//
//            }
//
//            @Override
//            public void onEndTabSelected(final NavigationTabBar.Model model, final int index) {
//                Toast.makeText(getActivity(), String.format("onEndTabSelected #%d", index), Toast.LENGTH_SHORT).show();
//            }
//        });
        workRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        workAdapter = new WorkAdapter(indos, this);
        workAdapter.setOnItemClickListener(new OnItemClickListener<Indo>() {

            @Override
            public void onClick(View view, Indo item) {

            }
        });

        workAdapter.setOnItemLongClickListener(new OnItemLongClickListener<Indo>() {
            @Override
            public void onLongClick(View view, Indo item) {
            }
        });
        workRecyclerView.setHasFixedSize(true);//固定大小
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        workRecyclerView.setLayoutManager(linearLayoutManager);
        //间隔线条
//        workRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        workRecyclerView.setAdapter(workAdapter);

        //拽拉列表控件
        ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(workAdapter);
        mItemTouchHelper = new ItemTouchHelper(callback);
        mItemTouchHelper.attachToRecyclerView(workRecyclerView);
        workRecyclerView.addOnScrollListener(new HidingScrollListener() {
            @Override
            public void onHide() {
                hideViews();
            }

            @Override
            public void onShow() {
                showViews();
            }
        });

        isPrepared = true;
        Log.e("TAG", "onCreateView-I");
        lazyLoad();//这里我们调用以下去加载我们的数据
        return view;
    }

    private void hideViews() {
        navigationTabBar.animate().translationY(-navigationTabBar.getHeight()).setInterpolator(new AccelerateInterpolator(2)).start();


        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) frameLayout.getLayoutParams();
        int fabBottomMargin = lp.bottomMargin;
        frameLayout.animate().translationY(frameLayout.getHeight() + fabBottomMargin).setInterpolator(new AccelerateInterpolator(2)).start();


        FrameLayout.LayoutParams lp2 = (FrameLayout.LayoutParams)  bg_ntb_horizontal.getLayoutParams();
        int fabBottomMargin2 = lp2.bottomMargin;
        bg_ntb_horizontal.animate().translationY(bg_ntb_horizontal.getHeight() + fabBottomMargin2).setInterpolator(new AccelerateInterpolator(2)).start();

    }

    private void showViews() {
        navigationTabBar.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start();
        frameLayout.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start();
        bg_ntb_horizontal.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start();


    }

    @Override
    public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
        mItemTouchHelper.startDrag(viewHolder);
    }

    ItemTouchHelper mItemTouchHelper;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //3.
        Log.e("TAG", "onViewCreated=I");

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("TAG", "onDestroy-end-I");
    }

    //标志位，标志已经初始化完成。
    private boolean isPrepared;

    @Override
    protected void lazyLoad() {
        if (isPrepared && isVisible) {
            //网络请求加载....
            Log.e("TAG", "lazyLoad-if-I");
            Log.e("TAG", mIndexActionsListener == null ? "1" : "0");

            Observer<ArrayList<Indo>> observer = new Observer<ArrayList<Indo>>() {
                @Override
                public void onCompleted() {
                    Log.i("TAFG", "onCompleted-");
                }

                @Override
                public void onError(Throwable e) {
                    Log.i("TAFG", "onError-" + e.toString());
                }

                @Override
                public void onNext(ArrayList<Indo> indoss) {
                    Log.i("TAFG", "onNext-" + indoss.size());
                    for (Indo in : indoss) {

                    }
                    workAdapter.addList(indoss);
                }
            };
            mIndexActionsListener.requestIndex(observer);
            isPrepared = false;

        }
        Log.e("TAG", "lazyLoad-I-end");
    }
}
